package com.tqjc.system.C24012.core.config.websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @Author: 86195
 * @CreateTime: 2024-07-04  15:51
 * @Description: TODO
 * @Version: 1.0
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketMessageBrokerConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic"); // 设定客户端订阅的主题前缀
        registry.setApplicationDestinationPrefixes("/app"); // 设定服务器接收消息的前缀
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/websocket-endpoint")
//                .setAllowedOrigins("*")
                .setAllowedOriginPatterns("*")
                .withSockJS(); // 使用SockJS作为WebSocket的备选方案
    }
}
